﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Jinndev {

    /// <summary>
    /// 当前选中的地图中的物体
    /// </summary>
    public class MapCurObjPanel : MonoBehaviour {

        public System.Action<MapTileObjUI> onSelect;
        public System.Action onClose;

        public InputField inputSlotID;
        public Dropdown dropdownSlotID;
        public InputField inputLayer;
        public Dropdown dropdownLayer;
        public Text textCoord;
        public Text textInfo;

        [HideInInspector]
        public MapTileObjUI tileObj;

        private List<string> slotList = new List<string>();

        private void Awake() {
            UIUtil.AddButtonClick(transform, "Button Close", Hide);

            UIUtil.AddButtonClick(inputSlotID, "Button Option", OnClickSlotOption);
            UIUtil.AddButtonClick(inputLayer, "Button Option", OnClickLayerOption);
            UIUtil.AddButtonClick(transform, "Button Up", OnClickUp);
            UIUtil.AddButtonClick(transform, "Button Down", OnClickDown);
            UIUtil.AddButtonClick(transform, "Button Move", OnClickMove);

            dropdownSlotID.onValueChanged.AddListener(OnSelectOption);

            var list = new List<string>();
            for (int i = 0; i < MapEditorUI.MAX_LAYER; i++) {
                list.Add((i + 1).ToString());
            }
            dropdownLayer.ClearOptions();
            dropdownLayer.AddOptions(list);
            dropdownLayer.onValueChanged.AddListener(OnSelectLayer);
        }

        private void Start() {
            Hide();
        }

        private void Update() {

        }

        public void Show(MapTileObjUI tileObj) {
            DeselectCurrent();
            this.tileObj = tileObj;

            gameObject.SetActive(true);

            tileObj.ToggleSelected(true);
            textCoord.text = $"X: {tileObj.data.index.x}   Y: {tileObj.data.index.y}";
            textInfo.text = $"{tileObj.data.id}";

            inputSlotID.text = tileObj.data.slot;

            inputLayer.text = (tileObj.layer + 1) + "";
            dropdownLayer.SetValueWithoutNotify(tileObj.layer);

            onSelect?.Invoke(tileObj);
        }

        public void Hide() {
            DeselectCurrent();
            gameObject.SetActive(false);
            onClose?.Invoke();
        }

        private void DeselectCurrent() {
            if (tileObj != null) {
                if (tileObj.data.slot != inputSlotID.text) {
                    tileObj.data.slot = string.IsNullOrWhiteSpace(inputSlotID.text) ? null : inputSlotID.text;
                    EventManager.Invoke(CustomEvent.ChangeObjSlot);
                }
                tileObj.ToggleSelected(false);
                tileObj = null;
            }
        }

        /// <summary> 点击槽位选项 </summary>
        private void OnClickSlotOption() {
            slotList.Clear();
            slotList.Add("(空)");

            int selected = 0;

            MapLayerData activeLayerData = MapEditorUI.Instance.ActiveMapLayerData;
            if (activeLayerData != null) {
                foreach (var data in activeLayerData.dataList) {
                    if (!string.IsNullOrWhiteSpace(data.slot) && !slotList.Contains(data.slot)) {
                        if (data.slot == tileObj.data.slot) {
                            selected = slotList.Count;
                        }
                        slotList.Add(data.slot);
                    }
                }
            }
            dropdownSlotID.ClearOptions();
            dropdownSlotID.AddOptions(slotList);
            dropdownSlotID.SetValueWithoutNotify(selected);
            dropdownSlotID.Show();
        }

        private void OnClickLayerOption() {
            dropdownLayer.Show();
        }

        private void OnSelectOption(int index) {
            if (index == 0) {
                inputSlotID.text = null;
            }
            else {
                inputSlotID.text = slotList[index];
            }
        }

        /// <summary>切换层级</summary>
        private void OnSelectLayer(int index) {
            var oldLayerData = MapEditorUI.Instance.MapData.layers[tileObj.layer];
            var newLayerData = MapEditorUI.Instance.MapData.layers[index];

            var oldLayer = MapEditorUI.Instance.Layers[tileObj.layer];
            var newLayer = MapEditorUI.Instance.Layers[index];

            // TODO 没有判断index是否重复
            var data = tileObj.data;
            oldLayerData.dataList.Remove(data);
            newLayerData.dataList.Add(data);

            oldLayer.RemoveTile(data);
            newLayer.AddTile(data, true);

            Hide();
        }

        /// <summary>
        /// 上一层显示
        /// </summary>
        private void OnClickUp() {
            int index = tileObj.transform.GetSiblingIndex();
            if (index < tileObj.transform.parent.childCount - 1) {
                tileObj.transform.SetSiblingIndex(index + 1);
            }
        }

        /// <summary>
        /// 下一层显示
        /// </summary>
        private void OnClickDown() {
            int index = tileObj.transform.GetSiblingIndex();
            if (index > 0) {
                tileObj.transform.SetSiblingIndex(index - 1);
            }
        }

        private void OnClickMove() {
            MapEditorUI.Instance.MoveTile(tileObj.data);
        }

    }

}